package com.aoxin.controller.course.webCourse;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.SlfArticle;
import com.aoxin.service.inter.course.webCourse.ArticleService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

@Controller
@ResponseBody
@RequestMapping("/web/article")
public class ArticleController {

	@Autowired
	private ArticleService slfArticleService;

	/**
	 * 添加文章
	 * 
	 * @param slfArticle
	 * @param ajaxRes
	 */
	@RequestMapping("/insertSlfArticle")
	@LogInfo(operateType = OperateType.INSERT, content = "添加文章")
	public AjaxRes insertSlfArticle(SlfArticle slfArticle, AjaxRes ajaxRes) {
		Integer Id = ThreadLocalUtil.getEmployee().getEmployeeId();
		slfArticle.setEmployeeId(Id);
		try {
			if (slfArticle != null) {
				slfArticle.setActicleTxt(slfArticle.getActicleTxt().replaceAll("$aoxin", "#"));
				Integer integer = slfArticleService.insertSlfArticle(slfArticle);
				if (integer != 0) {
					ajaxRes.setSucceed(integer, Const.SAVE_SUCCEED);
				}
			}
		} catch (Exception e) {
			ajaxRes.setSucceed("/insertSlfArticle Failure");
			e.printStackTrace();
		}
		return ajaxRes;
	}

	/**
	 * 批量删除文章
	 * 
	 * @param articleId
	 * @param ajaxRes
	 */
	@RequestMapping("/deleteSlfArticle")
	@LogInfo(operateType = OperateType.DELETE, content = "删除文章")
	public AjaxRes deleteSlfArticle(@RequestParam("articleId") List<Integer> articleId, AjaxRes ajaxRes) {
		try {
			Integer integer = this.slfArticleService.deleteSlfArticle(articleId);
			if (integer != 0) {
				ajaxRes.setSucceed(Const.DEL_SUCCEED);
			} else {
				ajaxRes.setSucceed(Const.SUBM_FAIL);
			}
		} catch (Exception e) {
			ajaxRes.setSucceed("deleteSlfArticle Failure");
			e.printStackTrace();
		}
		return ajaxRes;
	}

	/**
	 * 修改文章
	 * 
	 * @param slfArticle
	 * @param ajaxRes
	 */
	@RequestMapping("/updateSlfArticle")
	@LogInfo(operateType = OperateType.UPDATE, content = "修改文章")
	public AjaxRes updateSlfArticle(SlfArticle slfArticle, AjaxRes ajaxRes) {
		try {
			slfArticle.setActicleTxt(slfArticle.getActicleTxt().replaceAll("$aoxin", "#"));
			Integer integer = this.slfArticleService.updateSlfArticle(slfArticle);
			if (integer != 0) {
				ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
			}
		} catch (Exception e) {
			ajaxRes.setSucceed("updateSlfArticle Failure");
			e.printStackTrace();
		}
		return ajaxRes;
	}

	/**
	 * 文章查询
	 * 
	 * @param slfArticle
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/getSlfArticle")
	@LogInfo(operateType = OperateType.QUERY, content = "查询文章")
	public AjaxRes getSlfArticle(SlfArticle slfArticle, AjaxRes ajaxRes) {
		List<SlfArticle> list = slfArticleService.getSlfArticle(slfArticle);
		ajaxRes.setSucceed(list, "getSlfArticle success");
		return ajaxRes;

	}

	@RequestMapping("/getBySlfArticle")
	@LogInfo(operateType = OperateType.QUERY, content = "查询文章")
	public AjaxRes getBySlfArticle(SlfArticle slfArticle, AjaxRes ajaxRes) {
		try {
			List<SlfArticle> list = slfArticleService.getBySlfArticle(slfArticle);
			ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;

	}

}
